local AvatarFactory = require("app.public.avatar.AvatarFactory")

local AvatarIconNode = class("AvatarIconNode",function(node) return node end)

-- 禁用 maskNodeName_
function AvatarIconNode:ctor(node,imageNodeName_,maskNodeName_)
    local imageNodeName = imageNodeName_ or "img_head"
    local maskNodeName = maskNodeName_ or "03Zhezhao"
    self._headImageNode = self:getChildByName(imageNodeName)

    -- 不再使用 ClipingNode
    -- 有遮罩 使用ClipingNode/sprite加载资源
    -- local maskNode = self:getChildByName(maskNodeName)
    -- if maskNode then
    --     maskNode:setVisible(false) --只是用来遮罩不需要显示

    --     local clipNode = cc.ClippingNode:create()
    --     clipNode:setInverted(true)
    --     clipNode:setAlphaThreshold(0.5)
    --     clipNode:setStencil(maskNode)
        
    --     local scale = maskNode:getScale()
    --     local size = maskNode:getContentSize()
    --     local sprite = cc.Sprite:create()
    --     sprite:setContentSize(size)
    --     sprite:setPosition(maskNode:getPosition())
    --     sprite:setScale(size.width * scale/90)
    --     sprite:setTag(1)
    --     clipNode:addChild(sprite)
    --     self._clipNode = clipNode
    --     self:addChild(self._clipNode)
    -- end
end

function AvatarIconNode:offSet(x,y)
    if self._headImageNode then
        local ox,oy = self._headImageNode:getPosition()
        self._headImageNode:setPosition(ox+x,oy+y)
    end
    if self._clipNode then
        local ox,oy = self._clipNode:getPosition()
        self._clipNode:setPosition(ox+x,oy+y)
    end
end

function AvatarIconNode:hide()
    if self._headImageNode then
        self._headImageNode:setVisible(false)
    end
    if self._clipNode then
        self._clipNode:setVisible(false)
    end
end

function AvatarIconNode:_update(params)
    if self._headImageNode then
        self._headImageNode:setVisible(false)
    end
    if self._clipNode then
        self._clipNode:setVisible(false)
    end

    AvatarFactory.getOrCreateHeroIconPath(params,function(path)
        if not tolua.isnull(self) then
            if self._clipNode then
                -- 有遮罩 使用ClipingNode加载资源
                local sprite = self._clipNode:getChildByTag(1)
                sprite:setTexture(path)
                self._clipNode:setVisible(true)
            elseif self._headImageNode then
                -- 没有遮罩 使用_headImageNode加载资源
                self._headImageNode:loadTexture(path)
                self._headImageNode:setVisible(true)
            end
        end
    end)
end

-- params = UserBaseInfo like
function AvatarIconNode:update(params)
    self:_update(params)
end

return AvatarIconNode